<?php
include('simple_html_dom.php');

function pulisci($col){
	$col = ereg_replace('<h2>' , '' ,$col );
	$col = ereg_replace('</h2>' , '' ,$col );
        $col = ereg_replace('<span>' , '' ,$col );
        $col = ereg_replace('</span.*>' , '' ,$col );
	$col = ereg_replace('<strong>' , '' ,$col);
	$col = ereg_replace('</strong>' , '' ,$col);
	$col = ereg_replace('<br>' , '' ,$col);
	$col = ereg_replace('<br />' , '' ,$col); 
	$col = ereg_replace('</div>' , '' ,$col);
	$col = ereg_replace('<span class="categoriaSciopero">' , '' ,$col);
	$col = ereg_replace('<div class="sottoTitoloScioperi">' , '' ,$col);
        $col = ereg_replace('<div class="dataSciopero">' , '' ,$col);
	$col = ereg_replace("<div class='infoScioperi'>" , '' ,$col);
        $col = str_replace("\t" , '' ,$col );
	return $col;
}

function ping($host, $port, $timeout) 
{ 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

function parser () {
   $json_tot = array ();
   $agg="";
   $html = file_get_html('http://www.mit.gov.it/mit/site.php?p=scioperi');   
   foreach ($html->find('div[id=boxScioperi]') as $row) {
       $agg=pulisci($row->find('div.sottoTitoloScioperi',0));
       $a=explode ( '<div class="dataSciopero">' , $row,20 );
       for ($i=1;$i<count($a); $i++){
          $json_parz=array();
          $rows='<div class="dataSciopero">'.$a[$i];
          $rows=str_get_html($rows);
          $data=pulisci($rows->find('div.dataSciopero',0));
          foreach ($rows->find('div.boxSciopero') as $row2) {
             $json_com = array();         
             $json_com['settore']=pulisci($row2->find('div.settoreSciopero',0)->find('strong',0));
             $json_com['categoria']=pulisci($row2->find('span.categoriaSciopero',0));
             $json_com['modalita']= pulisci($row2->find('div.modoSciopero',0)->find('strong',0));          
             $json_com['info']=pulisci($row2->find('div.infoScioperi',0));                             
             array_push($json_parz,$json_com);         
         }
         array_push($json_tot,array(
                               'data'=>$data,
                               'dettagli'=>$json_parz
                               )); 
      } 
   }
  $json=array(
   'aggiornamento'=>$agg,
   'dati'=>$json_tot
   );

  $myFile = "news.txt";
  $fh = fopen($myFile, 'w');
  $stringData = json_encode($json);
  fwrite($fh, $stringData);
  fclose($fh);
}
ping("www.mit.gov.it",80,10);

echo parser();


?>	